package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.util.Log;

import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.passanger.CouponCountBuild;
import com.bbx.api.sdk.model.passanger.LinePlusBuild;
import com.bbx.taxi.client.Bean.Line;
import com.bbx.api.sdk.net.base.BaseNetwork;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.CouponCountNet;
import com.bbx.api.sdk.net.passeger.conn.LinePlusNet;
import com.bbx.taxi.client.Bean.Message.ObserverListener;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.MyApplication;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 作者：严旭文 on 2016/12/21 16:22
 * 邮箱：420255048@qq.com
 */
public class MyLinePlusTask extends BaseTask{
    public ObserverListener.DATA_TYPE TYPE= ObserverListener.DATA_TYPE.LINE;
    private LinePlusBuild mLinePlusBuild;
    private boolean isShow;
    private LineDB db_line;
    public MyLinePlusTask(Activity context,boolean isShow) {
        super(context);
        setEncrypt(false);
        db_line=new LineDB();
        this.isShow=isShow;
        mLinePlusBuild = new LinePlusBuild(context);
        mLinePlusBuild.version=0;
    }
    @Override
    public boolean isShow() {
        return isShow;
    }

    @Override
    public boolean isReturnString() {
        return true;
    }
    @Override
    public boolean condition() {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public void request() {
        setType(TYPE,mLinePlusBuild);
    }
    @Override
    public void success(int status,Object object) {
        if(object!=null){
            try {
                db_line.onDelete();
                JSONObject JO=new JSONObject((String)object);
                JSONObject JO2=new JSONObject(JO.getString("cities"));
                JSONArray  ja_cities=JO2.getJSONArray("app_citys");
                for (int i = 0; i < ja_cities.length(); i++) {
                    try{
                    JSONObject jo = new JSONObject(ja_cities.get(i).toString());
                    String name=jo.getString("name");
                    String province=jo.getString("province");
                    String city=jo.getString("city");
                    String incity= Value.CITY_NOINCITY;
                    ChildCitysBeanX mChildCitysBeanX = null;
                    JSONArray  child_citys=jo.getJSONArray("child_citys");
                    if(child_citys.length()>0){
                         mChildCitysBeanX = (ChildCitysBeanX) new JsonBuild().getData(ChildCitysBeanX.class,
                                child_citys.get(0).toString());
                    }
                    if(mChildCitysBeanX==null)continue;

                    if(mChildCitysBeanX.getIs_city().equals(Value.CITY_INCITY)){
                        incity=Value.CITY_INCITY;
                    }
                    //出发地为show==0才存储
                        String ishow=mChildCitysBeanX.getIs_show();
                        if(ishow!=null&&ishow.equals(Value.CITY_NOSHOW)){
                            continue;
                        }
                        db_line.onWrite(city, name, province,incity,jo.toString());
                    }catch (Exception e){
                    }
                }

            }catch (Exception e){}
        }
    }
    @Override
    public void failed(int status, String msg,String message) {
        Log.e("xxxx",message+"");
    }
    @Override
    public BaseNetwork getBaseNetwork() {
        return new LinePlusNet(context);
    }

}
